#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2023-10-16 22:25
# @Author : hard work
# @File    : parse_snmp_util.py
# @Description :

from scapy.all import *

from common.remote_device_cfg import sw_profile
from utils.get_local_ip import get_host_ip
from utils.parse_pkt_utils.parse_pkt_base import Parse_pcap_base


class Snmp_utils:
    Parse = Parse_pcap_base(file="")

    def __init__(self, Wrireshark_path, iface="以太网"):
        # self.tshark_path = path_cfgs.file_path()[3] + "\\tshark.exe"
        self._Wrireshark_path = Wrireshark_path
        self._iface = iface
        self._pcap = None
        self.save_name = r'F:\Result\cap\snmp\snmpTrap.pcap'
        self.my_dict = {}

    def capture_snmp_trap(self, hight_layer='udp port snmp'):
        self._pcap = sniff(count=100, iface=self._iface, filter=hight_layer, timeout=100)
        print(self._pcap)

    def save_cap_snmp(self, save_name):
        """xxx.pcap"""
        if save_name is None:
            wrpcap(self.save_name, self._pcap)
        else:
            self.save_name = save_name
            wrpcap(save_name, self._pcap)

    def parse_snmp_trap(self):
        file = r'D:\Tools\ssss.pcap'
        p_lst = self.Parse.parse_pcap_to_one_list(self.save_name, self._Wrireshark_path)
        # p_lst = self.Parse.parse_pcap_to_one_list(file, self._Wrireshark_path)

        ip, _ = sw_profile()
        for pkt in p_lst:
            if pkt.ip.src == ip and pkt.ip.dst == get_host_ip():
            # if pkt.ip.src == '10.11.0.232' and pkt.ip.dst == '10.11.0.80':

                if pkt.transport_layer == 'UDP' and pkt.highest_layer == 'SNMP':
                    # print(dir(pkt))
                    # print(dir(pkt[pkt.highest_layer]))
                    # print(pkt.SNMP.value_ipv4)
                    # print(pkt.SNMP._all_fields)
                    # print(pkt.SNMP.name)
                    self.my_dict.update(pkt.SNMP._all_fields)
                return self.my_dict



if __name__ == '__main__':
    util = Snmp_utils(Wrireshark_path=r"D:\Tools\Wireshark")
    # util.capture_snmp_trap()
    # util.save_cap_snmp(r'F:\Result\cap\snmp\snmpTrap.pcap')
    oid = '1.3.6.1.4.1.49763.1.1.3.1.3'  # usrIp
    val = '10.11.0.217'
    myDict = util.parse_snmp_trap()
    for items in myDict.items():
        # print(items)

        for k in items:
            # print(k)

            if '1.3.6.1.2.1.1.3.0' in k:
                print(10 * '/')
                print(k)
            if '1.3.6.1.4.1.49763.1.2.3' in k:
                print(k)

            if '1.3.6.1.4.1.49763.1.1.3.1.3' in k:
                print(k)

                print(10 * '?')
